package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {
   @Autowired
   FileStorageService fileStorageService;
   @Autowired
   WmMaterialMapper wmMaterialMapper;
    @Override
    public ResponseResult uploadPicture(MultipartFile file) throws IOException {
        //检查参数
        if (file==null && file.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String originalFilename = file.getOriginalFilename();
        String perfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileId=null;
     fileId=   fileStorageService.uploadImgFile("",fileName+perfix,file.getInputStream());

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUrl(fileId);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        save(wmMaterial);



        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
//检查参数
        dto.checkParam();
        LambdaQueryWrapper<WmMaterial> wrapper=new LambdaQueryWrapper<>();
        //判断是否收藏
        if (dto.getIsCollection()!=null &&dto.getIsCollection()==1) {
            wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }


            //分页查询
        IPage<WmMaterial> ipage=new Page<>(dto.getPage(), dto.getSize());
        //按照用户查询
        wrapper.eq(WmMaterial ::getUserId,WmThreadLocalUtil.getUser().getId());
        //按时间查询
        wrapper.orderByDesc(WmMaterial ::getCreatedTime);
        IPage<WmMaterial> page = page(ipage, wrapper);
        //返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult updateWmMaterial(Integer id) {
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial ::getUserId,WmThreadLocalUtil.getUser().getId());
        wrapper.eq(WmMaterial ::getId,id);
        WmMaterial wmMaterial = wmMaterialMapper.selectOne(wrapper);
        wmMaterial.setIsCollection((short) 1);
        wmMaterialMapper.updateById(wmMaterial);


        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult updatecancel_collect(Integer id) {
//        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(WmMaterial ::getUserId,WmThreadLocalUtil.getUser().getId());
//        wrapper.eq(WmMaterial ::getId,id);
        WmMaterial wmMaterial = wmMaterialMapper.selectById(id);
        wmMaterial.setIsCollection((short) 0);
        wmMaterialMapper.updateById(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }
}









































